import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UploadServlet", value = "/UploadServlet")
@MultipartConfig
public class UploadServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;


    public UploadServlet(){
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        Part part = request.getPart("pic");
        String fileName = part.getSubmittedFileName();
        String savePath = getServletContext().getRealPath("/upload/");
        System.out.println("保存路径"+savePath);
        File f = new File(savePath+"/");
        if (!f.exists()){
            f.mkdirs();
        }
        part.write(savePath+"/"+fileName);

        PrintWriter out = response.getWriter();
        out.print("成功");
    }
}
